package cn.wolfcode.shop.message;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import com.alibaba.druid.sql.visitor.SchemaStatVisitor.OrderByStatVisitor;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;

import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.vo.OrderVo;

@Component
public class CreateMessageConsumer {

	@Reference
	private IOrderInfoService orderInfoService;

	/**
	 * 定义方法:当有消息进来就调用该方法
	 * 贴上@JmsListener注解就可以在配置地点有消息进来就会接收
	 * containerFactory 属性值代表需要监听什么类型的目的地
	 * @throws JMSException 
	 */
	@JmsListener(destination = "createOrderQueue", containerFactory = "jmsListenerContainerFactory")
	public void onMeassge(Message message) throws JMSException {
		TextMessage textMessage = (TextMessage) message;
		String content = textMessage.getText();
		OrderVo orderVo = JSON.parseObject(content, OrderVo.class);
		orderInfoService.createOrder(orderVo);
		System.out.println(orderVo);
	}

	// 多个监听是放在监听容器中的,,默认是监听queue的,topic消费不了
	@JmsListener(destination = "createOrderTopic", containerFactory = "jmsListenerContainerTopic")
	public void onMeassgeByTopic(Message message) throws JMSException {
		TextMessage textMessage = (TextMessage) message;
		String content = textMessage.getText();
		System.out.println(content);
	}
}
